package com.zbbt.sportslottery.util;

import java.io.File;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.TypedArray;
import android.os.Environment;
import android.os.Process;
import android.text.TextUtils;

import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;

public class AppUtil {
	public static final String TAG = "AppUtil";

	/**
	 * 
	 * @MethodName:getRootPath
	 * @Description: 获取应用跟目录
	 * @author：LiZhimin
	 * @date：2014-12-10 上午10:56:24
	 * @param context
	 * @return String
	 */
	public static String getRootPath(Context context) {
		String path = "";
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			path = Environment.getExternalStorageDirectory() + "/" + getPackageName(context) + "/";
		} else {
			// path = "/data/data/" + context.getPackageName() + "/";
			path = context.getFilesDir().getPath();
		}
		LogUtil.printI(Constant.APP_TAG, "path=" + path);
		return path;
	}

	/**
	 * 初始化应用的文件目录结构
	 */
	public static void initAppFileDir(final Context appContext) {
		if (TextUtils.isEmpty(App.ROOT_PATH)) {
			App.ROOT_PATH = AppUtil.getRootPath(appContext);
			File appPath = new File(App.ROOT_PATH);
			if (!appPath.exists()) {
				appPath.mkdir();
			}
		}
		if (TextUtils.isEmpty(App.OBJECT_CACHE_PATH)) {
			App.OBJECT_CACHE_PATH = App.ROOT_PATH + "object/";
			File objDir = new File(App.OBJECT_CACHE_PATH);
			if (!objDir.exists()) {
				objDir.mkdirs();
			}
		}

	}

	/**
	 * 
	 * @MethodName:getPackageInfo
	 * @Description: 获取内存卡上的apk文件的版本信息
	 * @author：LiZhimin
	 * @date：2014-3-19 下午3:10:50
	 * @param context
	 * @param apkPath
	 * @return PackageInfo
	 */
	public static PackageInfo getPackageInfo(Context context, String apkPath) {
		PackageManager pm = context.getPackageManager();
		File file = new File(apkPath);
		if (file.exists()) {
			try {
				return pm.getPackageArchiveInfo(file.getAbsolutePath(), PackageManager.GET_ACTIVITIES);
			} catch (Exception e) {
				LogUtil.defLog("解析本地包发生异常" + e.toString());
			}
		}
		return null;
	}

	/**
	 * 
	 * @MethodName:getVersionName
	 * @Description: 获取内存卡上apk文件的外部版本号
	 * @author：LiZhimin
	 * @date：2014-3-19 下午3:10:29
	 * @param context
	 * @param apkPath
	 * @return String
	 */
	public static String getVersionName(Context context, String apkPath) {
		PackageInfo packageInfo = getPackageInfo(context, apkPath);
		if (packageInfo != null) {
			return packageInfo.versionName;
		}
		return "0";
	}

	/**
	 * 
	 * @MethodName:getVersionCode
	 * @Description: 获取内存卡上apk文件的内部版本号
	 * @author：LiZhimin
	 * @date：2014-3-19 下午3:10:39
	 * @param context
	 * @param apkPath
	 * @return int
	 */
	public static int getVersionCode(Context context, String apkPath) {
		PackageInfo packageInfo = getPackageInfo(context, apkPath);
		if (packageInfo != null) {
			return packageInfo.versionCode;
		}
		return 0;
	}

	public static String getPackageName(Context context) {
		return context.getPackageName();
	}

	public static PackageInfo getPackageInfo(Context context) {
		PackageManager packageManager = context.getPackageManager();
		String packageName = context.getPackageName();
		try {
			return packageManager.getPackageInfo(packageName, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 
	 * @MethodName:getVersionName
	 * @Description: 应用外部版本号
	 * @author：LiZhimin
	 * @date：2014-12-26 下午1:15:06
	 * @param context
	 * @return String
	 */
	public static String getVersionName(Context context) {
		PackageInfo packageInfo = getPackageInfo(context);
		if (packageInfo != null) {
			return packageInfo.versionName;
		}
		return "0";
	}

	/**
	 * 
	 * @MethodName:getVersionCode
	 * @Description: 应用内部版本号
	 * @author：LiZhimin
	 * @date：2014-12-26 下午1:14:43
	 * @param context
	 * @return int
	 */
	public static int getVersionCode(Context context) {
		PackageInfo packageInfo = getPackageInfo(context);
		if (packageInfo != null) {
			return packageInfo.versionCode;
		}
		return 0;
	}

	/**
	 * 
	 * @MethodName:killProcess
	 * @Description: 杀死应用进程退出应用
	 * @author：LiZhimin
	 * @date：2014-3-19 下午6:00:21 void
	 */
	public static void killProcess() {
		Process.killProcess(Process.myPid());
	}

	/**
	 * 
	 * @MethodName:getApkFileName
	 * @Description: 获取内存卡上apk文件的外部版本号
	 * @author：LiZhimin
	 * @date：2014-3-19 下午3:10:29
	 * @param context
	 * @param apkPath
	 * @return String
	 */
	public static String getApkFileName(Context context, String apkPath) {
		return getApkFileInfo(context, apkPath)[1];
	}

	/**
	 * 
	 * @MethodName:getApkFileInfo
	 * @Description: 获取内存卡上的apk文件的版本信息
	 * @author：LiZhimin
	 * @date：2014-3-19 下午3:10:50
	 * @param context
	 * @param apkPath
	 * @return String[]
	 */
	public static String[] getApkFileInfo(Context context, String apkPath) {
		PackageManager pm = context.getPackageManager();
		// apkPath="/WZCP/1000/elottery.apk";
		File file = new File(apkPath);
		if (!file.exists()) {
			return new String[] { "", "" };
		}
		PackageInfo packageInfo = null;
		try {
			packageInfo = pm.getPackageArchiveInfo(file.getAbsolutePath(), PackageManager.GET_ACTIVITIES);
		} catch (Exception e) {
			LogUtil.defLog("解析本地包发生异常" + e.toString());
		}
		if (packageInfo != null) {
			final String versionCode = packageInfo.versionCode + "";
			final String versionName = packageInfo.versionName;
			LogUtil.defLog("versionCode:" + versionCode + "  versionName:" + versionName);
			return new String[] { versionCode, versionName };
		}
		return new String[] { "", "" };
	}

	/**
	 * 
	 * @MethodName:initCustomParam
	 * @Description: 初始化网络请求通用参数
	 * @author：LiZhimin
	 * @date：2015-2-27 下午3:12:01
	 * @param context
	 *            void
	 */
	public static void initCustomParam(Context context) {
		App.PARAM_BRAND = android.os.Build.BRAND;
		App.PARAM_NETWORK = NetUtil.getCurrentNetType(context);
		App.PARAM_SYSVER = android.os.Build.VERSION.RELEASE;
		App.PARAM_SOFTVER = getVersionName(context);
		App.PARAM_DEVICEID = Device.getUniqueIdentifier(context);
		App.PARAM_SID = getSourceId(context);
		LogUtil.printI(TAG, "手机品牌:" + App.PARAM_BRAND);
		LogUtil.printI(TAG, "网络状态:" + App.PARAM_NETWORK);
		LogUtil.printI(TAG, "系统版本:" + App.PARAM_SYSVER);
		LogUtil.printI(TAG, "应用版本:" + App.PARAM_SOFTVER);
		LogUtil.printI(TAG, "平台:" + App.PARAM_PLATFORM);
		LogUtil.printI(TAG, "设备id:" + App.PARAM_DEVICEID);
		LogUtil.printI(TAG, "渠道号:" + App.PARAM_SID);
	}

	/**
	 * 
	 * @MethodName:getSourceId
	 * @Description: 获取渠道号
	 * @author：LiZhimin
	 * @date：2015-2-27 下午4:35:40
	 * @param context
	 * @return String
	 */
	public static String getSourceId(Context context) {
		String sid = "";
		PackageManager packageManager = context.getPackageManager();
		String packageName = context.getPackageName();
		ApplicationInfo applicationInfo;
		try {
			applicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
			Object sourceIdObject = applicationInfo.metaData.get("UMENG_CHANNEL");
			if (sourceIdObject instanceof Integer) {
				sid = Integer.toString((Integer) sourceIdObject);
			} else if (sourceIdObject instanceof String) {
				sid = (String) sourceIdObject;
			} else {
				sid = "";
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return sid;

	}

	// 获取toobar高度
	public static int getToolbarHeight(Context context) {
		final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
				new int[] { R.attr.actionBarSize });
		int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
		styledAttributes.recycle();

		return toolbarHeight;
	}

	/**
	 * 
	 * @MethodName: setAppId
	 * @Description: 设置AppleId
	 * @author: yangwenquan
	 * @date: 2015年4月2日 下午5:29:06
	 * @param City
	 *            void
	 */
//	public static void setAppId(String City) {
//		switch (City) {
//		case "福建省":
//			App.PARAM_APPID = "1";
//			break;
//		case "北京市":
//			App.PARAM_APPID = "2";
//			break;
//		case "新疆维吾尔自治区":
//			App.PARAM_APPID = "3";
//			break;
//		case "西藏自治区":
//			App.PARAM_APPID = "4";
//			break;
//		case "四川省":
//			App.PARAM_APPID = "5";
//			break;
//		case "重庆市":
//			App.PARAM_APPID = "6";
//			break;
//		case "青海省":
//			App.PARAM_APPID = "7";
//			break;
//		case "内蒙古自治区":
//			App.PARAM_APPID = "8";
//			break;
//		case "贵州省":
//			App.PARAM_APPID = "9";
//			break;
//		case "云南省":
//			App.PARAM_APPID = "10";
//			break;
//		case "山西省":
//			App.PARAM_APPID = "11";
//			break;
//		case "黑龙江省":
//			App.PARAM_APPID = "12";
//			break;
//		case "辽宁省":
//			App.PARAM_APPID = "13";
//			break;
//		case "吉林省":
//			App.PARAM_APPID = "14";
//			break;
//		case "陕西省":
//			App.PARAM_APPID = "16";
//			break;
//		case "河南省":
//			App.PARAM_APPID = "17";
//			break;
//		case "河北省":
//			App.PARAM_APPID = "18";
//			break;
//		case "湖北省":
//			App.PARAM_APPID = "19";
//			break;
//		case "湖南省":
//			App.PARAM_APPID = "20";
//			break;
//		case "广东省":
//			App.PARAM_APPID = "21";
//			break;
//		case "广西壮族自治区":
//			App.PARAM_APPID = "22";
//			break;
//		case "江西省":
//			App.PARAM_APPID = "23";
//			break;
//		case "江苏省":
//			App.PARAM_APPID = "24";
//			break;
//		case "浙江省":
//			App.PARAM_APPID = "25";
//			break;
//		case "上海市":
//			App.PARAM_APPID = "26";
//			break;
//		case "山东省":
//			App.PARAM_APPID = "27";
//			break;
//		case "天津市":
//			App.PARAM_APPID = "28";
//			break;
//		case "宁夏回族自治区":
//			App.PARAM_APPID = "29";
//			break;
//		case "海南省":
//			App.PARAM_APPID = "30";
//			break;
//		case "安徽省":
//			App.PARAM_APPID = "31";
//			break;
//		case "甘肃省":
//			App.PARAM_APPID = "32";
//			break;
//		default:
//			App.PARAM_APPID = "1";
//			break;
//		}
//		LogUtil.printI(TAG, "AppId==" + App.PARAM_APPID);
//	}

	public static String getAppid(String cityname) {
		if (cityname.equals("福建省")) {
			return "1";
		}
		if (cityname.equals("北京市")) {
			return "2";
		}
		if (cityname.equals("新疆维吾尔自治区")) {
			return "3";
		}
		if (cityname.equals("西藏自治区")) {
			return "4";
		}
		if (cityname.equals("四川省")) {
			return "5";
		}
		if (cityname.equals("重庆市")) {
			return "6";
		}
		if (cityname.equals("青海省")) {
			return "7";
		}
		if (cityname.equals("内蒙古自治区")) {
			return "8";
		}
		if (cityname.equals("贵州省")) {
			return "9";
		}
		if (cityname.equals("云南省")) {
			return "10";
		}
		if (cityname.equals("山西省")) {
			return "11";
		}
		if (cityname.equals("黑龙江省")) {
			return "12";
		}
		if (cityname.equals("辽宁省")) {
			return "13";
		}
		if (cityname.equals("吉林省")) {
			return "14";
		}
		if (cityname.equals("陕西省")) {
			return "16";
		}
		if (cityname.equals("河南省")) {
			return "17";
		}
		if (cityname.equals("河北省")) {
			return "18";
		}
		if (cityname.equals("湖北省")) {
			return "19";
		}
		if (cityname.equals("湖南省")) {
			return "20";
		}
		if (cityname.equals("广东省")) {
			return "21";
		}
		if (cityname.equals("广西壮族自治区")) {
			return "22";
		}
		if (cityname.equals("江西省")) {
			return "23";
		}
		if (cityname.equals("江苏省")) {
			return "24";
		}
		if (cityname.equals("浙江省")) {
			return "25";
		}
		if (cityname.equals("上海市")) {
			return "26";
		}
		if (cityname.equals("山东省")) {
			return "27";
		}
		if (cityname.equals("天津市")) {
			return "28";
		}
		if (cityname.equals("宁夏回族自治区")) {
			return "29";
		}
		if (cityname.equals("海南省")) {
			return "30";
		}
		if (cityname.equals("安徽省")) {
			return "31";
		}
		if (cityname.equals("甘肃省")) {
			return "32";
		}
		return "1";
	}

	public static void getAndroidSDKVersion() {
		try {
			App.PARAM_SDK_VERSION = Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}
}
